import ListNode from './ListNode';
import { defaultEqualFunction } from '../../util/defaultEqualFunction';

export default class LinkedList<T> {
    head: ListNode<T> | null;
    count: number;
    equalFn: Function;

    constructor(equalFn = defaultEqualFunction) {
        this.count = 0;
        this.head = null;
        this.equalFn = equalFn;
    }

    append(element: T): void {
        const newNode = new ListNode<T>(element);
        if (!this.head) {
            this.head = newNode;
        } else {
            let current = this.head;
            while (current.next) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    prepend(element: T) {
        const newNode = new ListNode(element);
        newNode.next = this.head;
        this.head = newNode;
    }

    [Symbol.iterator]() {
        let current = this.head;
        return {
            next() {
                console.log(current)
                if (current) {
                    let res = current;
                    current = current.next;
                    return { done: false, value: res.element };
                } else {
                    return { done: true, value: undefined };
                }
            },
        };
    }

    toString() {
        return [...this].join(' -> ');
    }
}

const linkedList1 = new LinkedList();
linkedList1.append(1);
linkedList1.append(2);
linkedList1.append('a');
linkedList1.append('b');
linkedList1.append(true);
linkedList1.append(false);
console.log(linkedList1.toString());
// const linkedList2 = new LinkedList();
// linkedList2.prepend(1);
// linkedList2.prepend(2);
// linkedList2.prepend('a');
// linkedList2.prepend('b');
// linkedList2.prepend(true);
// linkedList2.prepend(false);
// console.log(linkedList2.toString());
